package com.pignest.hotel.definition;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.pignest.hotel.beans.Room;

/**
 * 房间状态
 * 
 * 房间含三种状态, 使用状态(RoomUseStatus), 入住状态(RoomLiveStatus), 维修状态(RoomRepairStatus)
 * 这里综合常用的状态组合, 方便过滤/查询
 * */
/** 
 *  入住状态: 入住, 未入住
 *  使用状态: 干净, 赃, 空隔夜, 维修, 停用, 
 *  使用类型: 自住, 散客, 长包, 时租, 预订, 
 *  使用方式: 团队, 个人, 协议, 
 *  
 *  汉庭
 *  1, 团队, 今日将到, 今日将离, 时租, 时租超时, 临时锁, 自用房, 无停留
 *  2, 检查, 空干净, 住干净, 空隔夜, 脏房, 在住脏, 换床单, 维修
 * */
public class RoomStatus {
	
	public static final RoomStatus EMPTY_CLEAN = new RoomStatus("空干净");
	public static final RoomStatus USE_CLEAN = new RoomStatus("住干净");
	public static final RoomStatus EMPTY_DIRTY = new RoomStatus("空脏房");
	public static final RoomStatus USE_DIRTY = new RoomStatus("在住脏");
	public static final RoomStatus EMPTY_NIGHT = new RoomStatus("空隔夜");
	public static final RoomStatus REPAIR = new RoomStatus("维修");
	//
	public static final RoomStatus SELF_HELP = new RoomStatus("自助房");
	public static final RoomStatus BOOKING = new RoomStatus("可预订");
	
	/** 系统状态 */
//	private static final RoomStatus[] SYSTEM_STATUSES = {
//		EMPTY_CLEAN, USE_CLEAN, EMPTY_DIRTY, USE_DIRTY, EMPTY_NIGHT, REPAIR
//	};
	
	private static final Map<String, RoomStatus> STATUS_MAP = new LinkedHashMap<String, RoomStatus>();
	
	static {
		EMPTY_CLEAN.set(RoomUseStatus.CLEAN);
		EMPTY_CLEAN.set(RoomLiveStatus.EMPTY);
		EMPTY_CLEAN.set(RoomRepairStatus.NORMAL);

		USE_CLEAN.set(RoomUseStatus.CLEAN);
		USE_CLEAN.set(RoomLiveStatus.EMPTY);
		USE_CLEAN.set(RoomRepairStatus.NORMAL);
		USE_CLEAN.set(false, true, false);
		
		EMPTY_DIRTY.set(RoomUseStatus.DIRTY);
		EMPTY_DIRTY.set(RoomLiveStatus.EMPTY);
		EMPTY_DIRTY.set(RoomRepairStatus.NORMAL);
		
		USE_DIRTY.set(RoomUseStatus.DIRTY);
		USE_DIRTY.set(RoomLiveStatus.EMPTY);
		USE_DIRTY.set(RoomRepairStatus.NORMAL);
		USE_DIRTY.set(false, true, false);
		
		EMPTY_NIGHT.set(RoomUseStatus.EMPTY_NIGHT);
		EMPTY_NIGHT.set(RoomLiveStatus.EMPTY);
		EMPTY_NIGHT.set(RoomRepairStatus.NORMAL);
		
		REPAIR.set(RoomRepairStatus.REPAIR);
		
		STATUS_MAP.put(EMPTY_CLEAN.name, EMPTY_CLEAN);
		STATUS_MAP.put(USE_CLEAN.name, USE_CLEAN);
		STATUS_MAP.put(EMPTY_DIRTY.name, EMPTY_DIRTY);
		STATUS_MAP.put(USE_DIRTY.name, USE_DIRTY);
		STATUS_MAP.put(EMPTY_NIGHT.name, EMPTY_NIGHT);
		STATUS_MAP.put(REPAIR.name, REPAIR);
		
//		for (int i = 0; i < SYSTEM_STATUSES.length; i++) {
//			STATUS_MAP.put(SYSTEM_STATUSES[i].name, SYSTEM_STATUSES[i]);
//		}
		//可自助的条件
		SELF_HELP.set(RoomLiveStatus.EMPTY);
		SELF_HELP.set(RoomRepairStatus.NORMAL);
		SELF_HELP.set(RoomUseStatus.CLEAN, RoomUseStatus.EMPTY_NIGHT);
		
		BOOKING.set(RoomLiveStatus.EMPTY);
		BOOKING.set(RoomRepairStatus.NORMAL);
	}
	
	public static Collection<RoomStatus> get() {
		return STATUS_MAP.values();
	}
	
	private String name;
	private RoomUseStatus[] use;
	private RoomLiveStatus[] live;
	private RoomRepairStatus[] repair;
	
	private boolean notUse;
	private boolean notLive;
	private boolean notRepair;
	
	private RoomStatus(String name) {
		this.name = name;
	}
	
	public RoomStatus(Room room) {
		if (room == null)
			return;
//		RoomUseStatus use = room.get
	}
	
	public void applyToRoom(Room room) {
		
	}
	
/*	RoomStatus(String name, RoomUseStatus use, RoomLiveStatus live, RoomRepairStatus repair) {
		this.name = name;
		this.use = use != null ? new RoomUseStatus[] {use} : null;
		this.live = live != null ? new RoomLiveStatus[] {live} : null;
		this.repair = repair != null ? new RoomRepairStatus[] {repair} : null;
	}
	
	RoomStatus(String name, RoomUseStatus[] use, RoomLiveStatus[] live, RoomRepairStatus[] repair) {
		this.name = name;
		this.use = use;
		this.live = live;
		this.repair = repair;
	}
*/
	
	private RoomStatus set(boolean use, boolean live, boolean repair) {
		this.notUse = use;
		this.notLive = live;
		this.notRepair = repair;
		return this;
	}
	
	RoomStatus set(RoomUseStatus... use) {
		this.use = use;
		return this;
	}
	
	RoomStatus set(RoomLiveStatus... live) {
		this.live = live;
		return this;
	}
	
	RoomStatus set(RoomRepairStatus... repair) {
		this.repair = repair;
		return this;
	}
	
	public String getName() {
		return name;
	}
	
	public RoomUseStatus[] getUseStatus() {
		return use;
	}
	
	public RoomLiveStatus[] getLiveStatus() {
		return live;
	}
	
	public RoomRepairStatus[] getRepairStatus() {
		return repair;
	}
	
	/** 房间是否为该状态 */
	public boolean is(Room room) {
		if (room == null)
			return false;
		boolean b = false;
		if (use != null && use.length > 0) {
			for (int i = 0; i < use.length; i++) {
				if (use[i].name().equals(room.getUseStatus()))
					if (notUse)
						return false;
					else {
						b = true;
						break;
					}
			}
			if (!notUse && !b)
				return false;
			b = false;
		}
		if (live != null && live.length > 0) {
			for (int i = 0; i < live.length; i++) {
				if (live[i].name().equals(room.getLiveStatus()))
					if (notLive)
						return false;
					else {
						b = true;
						break;
					}
			}
			if (!notLive && !b)
				return false;
			b = false;
		}
		if (repair != null && repair.length > 0) {
			for (int i = 0; i < repair.length; i++) {
				if (repair[i].name().equals(room.getRepairStatus()))
					if (notRepair)
						return false;
					else {
						b = true;
						break;
					}
			}
			if (!notRepair && !b)
				return false;
		}
		return true;
	}
	
	public String toHql(String table) {
		StringBuilder sb = new StringBuilder();
		if (use != null && use.length > 0) {
			for (int i = 0; i < use.length; i++) {
				if (i == 0) {
					if (notUse)
						sb.append(" (not");
					sb.append(" (");
				} else {
					sb.append(" or");
				}
				if (table != null)
					sb.append(table).append('.');
				sb.append(" useStatus=").append('\'').append(use[i].name()).append('\'');
				if (i == use.length -1) {
					sb.append(')');
					if (notUse)
						sb.append(')');
				}
			}
		}
		if (live != null && live.length > 0) {
			if (sb.length() > 0)
				sb.append(" and");
			for (int i = 0; i < live.length; i++) {
				if (i == 0) {
					if (notLive)
						sb.append(" (not");
					sb.append(" (");
				} else {
					sb.append(" or");
				}
				if (table != null)
					sb.append(table).append('.');
				sb.append(" liveStatus=").append('\'').append(live[i].name()).append('\'');
				if (i == live.length -1) {
					sb.append(')');
					if (notLive)
						sb.append(')');
				}
			}
		}
		if (repair != null && repair.length > 0) {
			if (sb.length() > 0)
				sb.append(" and");
			for (int i = 0; i < repair.length; i++) {
				if (i == 0) {
					if (notRepair)
						sb.append(" (not");
					sb.append(" (");
				} else {
					sb.append(" or");
				}
				if (table != null)
					sb.append(table).append('.');
				sb.append(" repairStatus=").append('\'').append(repair[i].name()).append('\'');
				if (i == repair.length -1) {
					sb.append(')');
					if (notRepair)
						sb.append(')');
				}
			}
		}
		if (sb.length() > 0) {
			sb.insert(0, '(');
			sb.append(')');
		}
		return sb.toString();
	}
	
	public static boolean match(List<String> statuses, Room room) {
		if (statuses == null || statuses.isEmpty())
			return true;
		for (String status : statuses) {
			RoomStatus rs = STATUS_MAP.get(status);
			if (rs == null)
				continue;
			if (rs.is(room))
				return true;
		}
		return false;
	}
}
